package com.zhenyi.jiahua.index.admin;

import java.util.List;

import com.jfinal.kit.Kv;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.SqlPara;
import com.jiahua.common.model.JhChannel;
import com.utils.PathUtils;
import com.zhenyi.jiahua.index.JHController;

import cn.hutool.core.io.file.FileWriter;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;

/**
 * 渠道管理
 * 
 * @author Zhongxian Li at 2018年10月29日 下午3:14:21
 *
 */
public class ChannelController extends JHController {

	public void list() {
		Long channelId = getParaToLong("channelId");
		SqlPara sqlPara;
		if (channelId == null) {
			sqlPara = Db.use("jiahua").getSqlPara("JHFindRootChannel");
		} else {
			Kv kv = Kv.create();
			kv.set("channelId", channelId);
			sqlPara = Db.use("jiahua").getSqlPara("JHFindChildrenChannel", kv);
		}
		Page<Record> page = Db.use("jiahua").paginate(getPage(), PAGE_SIZE, sqlPara);
		setRainbow(page);
		setItems(JSONUtil.toJsonStr(page.getList()));
		setAttr("channelId", channelId);
		render("/jh/admin/channel/index.html");
	}

	public void info() {
		Long channelId = getParaToLong("channelId");
		if (channelId == null) {
			renderFailure(500, "id is null");
			return;
		}
		JhChannel info = JhChannel.dao.findById(channelId);
		if (info == null) {
			renderFailure(404, "channel not found");
			return;
		}
		renderSuccess(info);
	}

	public void submit() {
		Long id = getParaToLong("id");
		String name = getPara("name");
		Long channelId = getParaToLong("channelId");
		boolean isNew = id == null;
		JhChannel info = isNew ? new JhChannel() : JhChannel.dao.findById(id);
		if (!isNew && info == null) {
			renderFailure(404, "channel not found");
			return;
		}
		info.setName(name);
		info.setChannelId(channelId);
		boolean flag = isNew ? info.save() : info.update();
		if (flag) {
			genChannelJson();
		}
		renderFlag(flag);
	}

	public void delete() {
		Long channelId = getParaToLong("channelId");
		if (channelId == null) {
			renderFailure(500, "id is null");
			return;
		}
		JhChannel info = JhChannel.dao.findById(channelId);
		if (info == null) {
			renderFailure(404, "channel not found");
			return;
		}
		renderFlag(info.delete());
	}

	private void genChannelJson() {
		SqlPara sqlPara = Db.use("jiahua").getSqlPara("JHFindRootChannel");
		List<JhChannel> rootChannels = JhChannel.dao.find(sqlPara);
		JSONArray jsonArray = new JSONArray();
		for (JhChannel rootChan : rootChannels) {
			JSONObject jsonObj = new JSONObject();
			jsonObj.put("label", rootChan.getName());
			jsonObj.put("value", rootChan.getId().toString());

			SqlPara sqlPara2 = Db.use("jiahua").getSqlPara("JHFindChildrenChannel",
					Kv.by("channelId", rootChan.getId()));
			List<JhChannel> childChans = JhChannel.dao.find(sqlPara2);
			if (childChans != null && childChans.size() > 0) {
				genChildChannelJson(jsonObj, childChans);
			}
			jsonArray.put(jsonObj);
		}
		// String filePath = PathKit.getWebRootPath() + File.separatorChar +
		// "jh" + File.separatorChar + "json"
		// + File.separatorChar + "channel.json";
		String filePath = PathUtils.getFile("jh", "json", "channel.json");
		FileWriter writer = new FileWriter(filePath);
		writer.write("var channel = " + jsonArray.toString());
	}

	private void genChildChannelJson(JSONObject channel, List<JhChannel> childList) {
		JSONArray array = new JSONArray();
		for (JhChannel child : childList) {
			JSONObject obj = new JSONObject();
			obj.put("label", child.getName());
			obj.put("value", child.getId().toString());

			SqlPara sqlPara = Db.use("jiahua").getSqlPara("JHFindChildrenChannel", Kv.by("channelId", child.getId()));
			List<JhChannel> childChans = JhChannel.dao.find(sqlPara);
			if (childChans != null && childChans.size() > 0) {
				genChildChannelJson(obj, childChans);
			}
			array.put(obj);
		}
		channel.put("children", array);
	}

}
